package jsen.tool.ddd.handle.graph;

import com.google.common.collect.Sets;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author jsen
 * @date 2021/10/6
 */
@Deprecated
public class Graph<D> {
    private final List<Node<D>> nodeList = new ArrayList<>(8);

    public void addNode(Node<D> node) {
        nodeList.add(node);
    }

    public void linkTo(Node<D> from, Node<D> to) {
        from.linkTo(to);
    }

    public void dfs(Consumer<Node<D>> consumer) {
        Queue<Node<D>> nodeQueue = nodeList.stream().filter(Node::isRoot)
                .collect(Collectors.toCollection(LinkedList::new));
        Set<Node<D>> visited = Sets.newHashSet();
        while (!nodeQueue.isEmpty()) {
            Node<D> node = nodeQueue.poll();
            if (visited.contains(node)) {
                continue;
            }
            visited.add(node);
            consumer.accept(node);
            nodeQueue.addAll(node.getAllOutNode());
        }
    }
}
